package com.wang.vedio.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.picture.lib.tools.ScreenUtils;
import com.wang.vedio.R;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * @说明 自定义导航栏组件

 * @时间 2016-6-3 下午2:22:14
 * @参数
 */
public class TabPageIndicator extends HorizontalScrollView {
	//tab按钮是否可点击
	private boolean couldClick= true;
	// 选中后tab标题是否变大
	private boolean isSelectTextBeginBig = false;
	// tab标题字体大小
	private int normalTextSize = 16;
	// tab标题中选中后变大的字体大小
	private int bigTextSize = 18;
	// tab标题数组
	private String[] titles = null;
	// tab标题选中后的图标
	private int[] selectTabIcon = null;
	// tab没有选中的图标
	private int[] unSelectTabIcon = null;
	// 图标排列的位置：在字体左边，在字体上边，在字体右边，在字体下边
	private DIRECTION direction = DIRECTION.TOP;
	// 屏幕宽度
	private int screenWidth = ScreenUtils.getScreenWidth(getContext());
	// 默认的tab的宽度
	private int tabWidth = ScreenUtils.getScreenWidth(getContext()) / 4;
	// 默认的tab的高度
	private int tabHeight = ScreenUtils.getScreenWidth(getContext()) / 8;
	// tab字体选中的颜色
	private int selectTextColor = R.color.tab_menu_text_select;
	// tab字体不选中的颜色
	private int unSelectTextColor = R.color.tab_menu_text_normal;
	// tab选中的背景图片
	private int selectBgResId = R.mipmap.gyss_tab1_select;
	// tab不选中的背景图片
	private int unSelectBgResId = R.mipmap.gyss_tab1_normal;
	// 与tab合用的viewPager
	private MyViewPager mViewPager;
	// tab父linearlayout
	private LinearLayout TabParentLinearLayout;
	// 自定义选中的tabLinearLayout
	private MyLinearLayout mLinearLayoutSelected;
	
	public View getTabView(int index){
		if(TabParentLinearLayout != null){
			return TabParentLinearLayout.getChildAt(index);
		}
		return null;
	}

	/**
	 * @说明 枚举类

	 * @时间 2016-6-3 下午2:23:45
	 * @参数
	 */
	public enum DIRECTION {
		TOP, BOTTOM, LEFT, RIGHT
	}

	public TabPageIndicator(Context context) {
		this(context, null);
	}

	public TabPageIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		TabParentLinearLayout = new LinearLayout(context);
		addView(TabParentLinearLayout, new ViewGroup.LayoutParams(WRAP_CONTENT, MATCH_PARENT));
	}

	/**
	 * @说明 设置tab的宽度(这里用一句话描述这个方法的作用)

	 * @param tabWidth
	 * @return
	 */
	public TabPageIndicator setTabWidth(int tabWidth) {
		this.tabWidth = tabWidth;
		return this;
	}

	/**
	 * @说明 设置tab的高度(这里用一句话描述这个方法的作用)

	 * @param tabHeight
	 * @return
	 */
	public TabPageIndicator setTabHeight(int tabHeight) {
		this.tabHeight = tabHeight;
		return this;
	}

	/**
	 * @说明 设置tab标题的大小(这里用一句话描述这个方法的作用)

	 * @param textSize
	 * @return
	 */
	public TabPageIndicator setNormalTextSize(int textSize) {
		this.normalTextSize = textSize;
		return this;
	}

	/**
	 * @说明 设置标题选中后是否会变大，和变大的字体大小(这里用一句话描述这个方法的作用)

	 * @param textSize
	 * @param big
	 * @return
	 */
	public TabPageIndicator setBigTextSize(int textSize, boolean big) {
		this.bigTextSize = textSize;
		this.isSelectTextBeginBig = big;
		return this;
	}
	
	public TabPageIndicator setCouldClick(boolean couldClick) {
		this.couldClick = couldClick;
		if(mViewPager != null){
			mViewPager.setScrollble(couldClick);
		}
		return this;
	}

	/**
	 * @说明 设置tab选中的图标的数组集合(这里用一句话描述这个方法的作用)

	 * @param selectTabIcon
	 * @return
	 */
	public TabPageIndicator setSelectTabIcon(int[] selectTabIcon) {
		this.selectTabIcon = selectTabIcon;
		return this;
	}

	/**
	 * @说明 设置tab不选中的图标的数组集合(这里用一句话描述这个方法的作用)

	 * @param unSelectTabIcon
	 * @return
	 */
	public TabPageIndicator setUnSelectTabIcon(int[] unSelectTabIcon) {
		this.unSelectTabIcon = unSelectTabIcon;
		return this;
	}

	/**
	 * @说明 设置tab选中后的字体颜色(这里用一句话描述这个方法的作用)

	 * @param selectTextColor
	 * @return
	 */
	public TabPageIndicator setSelectTextColor(int selectTextColor) {
		this.selectTextColor = selectTextColor;
		return this;
	}

	/**
	 * @说明 TODO(这里用一句话描述这个方法的作用)

	 * @param unSelectTextColor
	 * @return
	 */
	public TabPageIndicator setUnSelectTextColor(int unSelectTextColor) {
		this.unSelectTextColor = unSelectTextColor;
		return this;
	}

	/**
	 * @说明 设置tab标题(这里用一句话描述这个方法的作用)

	 * @param titles
	 * @return
	 */
	public TabPageIndicator setTitles(String[] titles) {
		this.titles = titles;
		return this;
	}

	/**
	 * @说明 选中的背景图片(这里用一句话描述这个方法的作用)

	 * @param selectBgIcon
	 * @return
	 */
	public TabPageIndicator setSelectBgIcon(int selectBgIcon) {
		this.selectBgResId = selectBgIcon;
		return this;
	}

	/**
	 * @说明 不选中的背景图片(这里用一句话描述这个方法的作用)

	 * @param unSelectBgIcon
	 * @return
	 */
	public TabPageIndicator setUnSelectBgIcon(int unSelectBgIcon) {
		this.unSelectBgResId = unSelectBgIcon;
		return this;
	}

	/**
	 * @说明 设置viewpager(这里用一句话描述这个方法的作用)

	 * @param view
	 */
	public void setViewPager(MyViewPager view) {
		TabParentLinearLayout.removeAllViews();
		if (mViewPager != null) {
			mViewPager.addOnPageChangeListener(null);
		}
		final PagerAdapter adapter = view.getAdapter();
		if (adapter == null) {
			throw new IllegalStateException("ViewPager does not have adapter instance.");
		}
		mViewPager = view;
		view.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				if (mLinearLayoutSelected != null) {
					mLinearLayoutSelected.getTitleView().setTextColor(getResources().getColor(unSelectTextColor));
					mLinearLayoutSelected.getTitleView().setTextSize(normalTextSize);
					if (unSelectTabIcon != null) {
						mLinearLayoutSelected.getImageView().setImageResource(unSelectTabIcon[mLinearLayoutSelected.getIndex()]);
					}
					if (unSelectBgResId != -1) {
						mLinearLayoutSelected.setBackgroundResource(unSelectBgResId);
					}
				}
				if (TabParentLinearLayout.getChildAt(arg0) != null) {
					MyLinearLayout myTabLinear = (MyLinearLayout) TabParentLinearLayout.getChildAt(arg0);
					myTabLinear.getTitleView().setTextColor(getResources().getColor(selectTextColor));
					if (isSelectTextBeginBig) {
						myTabLinear.getTitleView().setTextSize(bigTextSize);
					}
					if (unSelectTabIcon != null) {
						myTabLinear.getImageView().setImageResource(selectTabIcon[myTabLinear.getIndex()]);
					}
					if (unSelectBgResId != -1) {
						myTabLinear.setBackgroundResource(selectBgResId);
					}
					mLinearLayoutSelected = myTabLinear;
					int scrollPos = myTabLinear.getLeft() - (ScreenUtils.getScreenWidth(getContext()) - ScreenUtils.getScreenWidth(getContext()) / 3) / 2;
					if (scrollPos > 0) {
						smoothScrollTo(scrollPos, 0);
					} else {
						smoothScrollTo(0, 0);
					}
				}
				if(pageSelectListener != null){
					pageSelectListener.onPageSelected(arg0);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		int count = titles.length;
		for (int i = 0; i < count; i++) {
			String title = titles[i];
			if (title == null) {
				title = "";
			}
			addTabLinearView(i, title);
		}
		smoothScrollTo(0, 0);
		requestLayout();
	}

	/**
	 * @说明 添加tab导航按钮(这里用一句话描述这个方法的作用)

	 * @param index
	 * @param title
	 */
	private void addTabLinearView(int index, String title) {
		MyLinearLayout myLinear = new MyLinearLayout(getContext());
		myLinear.setGravity(Gravity.CENTER);
		myLinear.setPadding(0, 5, 0, 5);
		ImageView imageView = null;
		if (unSelectTabIcon != null) {
			imageView = createImageView(unSelectTabIcon[index]);
		}
		TextView titleTextView = createTitleTextView(title);
		myLinear.setIndex(index);
		if (unSelectBgResId != -1) {
			myLinear.setBackgroundResource(unSelectBgResId);
		}
		if (imageView != null) {
			if (direction == DIRECTION.TOP) {
				myLinear.setOrientation(LinearLayout.VERTICAL);
				myLinear.setImageView(imageView);
				myLinear.setTitleView(titleTextView);
			} else if (direction == DIRECTION.BOTTOM) {
				myLinear.setOrientation(LinearLayout.VERTICAL);
				myLinear.setTitleView(titleTextView);
				myLinear.setImageView(imageView);
			} else if (direction == DIRECTION.LEFT) {
				myLinear.setOrientation(LinearLayout.HORIZONTAL);
				myLinear.setImageView(imageView);
				myLinear.setTitleView(titleTextView);
			} else {
				myLinear.setOrientation(LinearLayout.HORIZONTAL);
				myLinear.setTitleView(titleTextView);
				myLinear.setImageView(imageView);
			}
		} else {
			myLinear.setTitleView(titleTextView);
		}
		myLinear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MyLinearLayout tabLayout = ((MyLinearLayout) v);
				int oldSelected = mViewPager.getCurrentItem();
				int newSelected = tabLayout.getIndex();
				if (oldSelected != newSelected) {
					if (tabClickListener != null) {
						Log.i("XXXXX","AAA");
						tabClickListener.onTabClick(tabLayout.getTitleView().getText().toString());
					}
					if (couldClick) {
						if(newSelected<mViewPager.getAdapter().getCount()){
							mLinearLayoutSelected.getTitleView().setTextColor(getResources().getColor(unSelectTextColor));
							mLinearLayoutSelected.getTitleView().setTextSize(normalTextSize);
							if (unSelectTabIcon != null) {
								mLinearLayoutSelected.getImageView().setImageResource(unSelectTabIcon[mLinearLayoutSelected.getIndex()]);
							}
							if (unSelectBgResId != -1) {
								mLinearLayoutSelected.setBackgroundResource(unSelectBgResId);
							}

							tabLayout.getTitleView().setTextColor(getResources().getColor(selectTextColor));
							if (isSelectTextBeginBig) {
								tabLayout.getTitleView().setTextSize(bigTextSize);
							}
							if (unSelectTabIcon != null) {
								tabLayout.getImageView().setImageResource(selectTabIcon[tabLayout.getIndex()]);
							}
							if (unSelectBgResId != -1) {
								tabLayout.setBackgroundResource(selectBgResId);
							}
							mLinearLayoutSelected = tabLayout;
							mViewPager.setCurrentItem(newSelected, false);
						}
					}
				}
			}
		});
		TabParentLinearLayout.addView(myLinear, index, new LinearLayout.LayoutParams(tabWidth, tabHeight));
		if (index == 0) {
			myLinear.getTitleView().setTextColor(getResources().getColor(selectTextColor));
			if (isSelectTextBeginBig) {
				myLinear.getTitleView().setTextSize(bigTextSize);
			}
			if (unSelectTabIcon != null) {
				myLinear.getImageView().setImageResource(selectTabIcon[0]);
			}
			if (unSelectBgResId != -1) {
				myLinear.setBackgroundResource(selectBgResId);
			}
			mLinearLayoutSelected = myLinear;
		}
	}

	/**
	 * @说明 生成TabIcon(这里用一句话描述这个方法的作用)

	 * @param tabIconId
	 * @return
	 */
	private ImageView createImageView(int tabIconId) {
		ImageView imageView = new ImageView(getContext());
		imageView.setPadding(20, 20, 20, 20);
		imageView.setImageResource(tabIconId);
		return imageView;
	}

	/**
	 * @说明 生成tabTitle(这里用一句话描述这个方法的作用)

	 * @param title
	 * @return
	 */
	private TextView createTitleTextView(String title) {
		TextView textView = new TextView(getContext());
		textView.setText(title);
		textView.setTextSize(normalTextSize);
		textView.setTextColor(getResources().getColor(unSelectTextColor));
		return textView;
	}

	/**
	 * @说明 自定义myLinearLayout

	 * @时间 2016-6-3 下午12:04:42
	 * @参数
	 */
	private class MyLinearLayout extends LinearLayout {
		private int index;
		private ImageView imageView;
		private TextView titleView;

		public MyLinearLayout(Context context) {
			super(context);
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}

		public ImageView getImageView() {
			return imageView;
		}

		public void setImageView(ImageView imageView) {
			addView(imageView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			this.imageView = imageView;
		}

		public TextView getTitleView() {
			return titleView;
		}

		public void setTitleView(TextView titleView) {
			addView(titleView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			this.titleView = titleView;
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	private TabClickListener tabClickListener = null;
	
	public void setOnTabClickListener( TabClickListener l){
		this.tabClickListener = l;
	}
	
	public interface TabClickListener {
		void onTabClick(String tabTitle);
	}
	
	
	private PageSelectListener pageSelectListener = null;
	
	public void setPageSelectListener( PageSelectListener l){
		this.pageSelectListener = l;
	}
	
	public interface PageSelectListener {
		void onPageSelected(int index);
	}


}
